package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.RelativeDateFormat;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.pojo.Visitors;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.CommentVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class QuanZiService {
    @Autowired
    private UserService userService;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private PicUploadService picUploadService;

    @Reference(version = "1.0.0")
    private VisitorsApi visitorsApi;

    public PageResult queryPublishList(Integer page, Integer pageSize) {
        // 分析:通过dubbo中的服务查询用户的好友状态
        // 通过mysql查询用户的信息,回写到结果对象中(QuanZiVo)
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page);

        // 直接去线程中获取User对象
        User user = UserThreadLocal.get();

        // 通过Dubbo查询发布内容的集合
        PageInfo<Publish> publishPageInfo = quanZiApi.queryPublishList(user.getId(), page, pageSize);
        List<Publish> records = publishPageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }

        pageResult.setItems(fillQuanZiVo(records));
        return pageResult;
    }

    /**
     * 封装User信息到QuanZiVo对象
     *
     * @param userInfo 要填充进quanZiVo的用户信息
     * @param quanZiVo 要被填充的QuanZiVo对象
     */
    private void fillUserInfoToQuanZiVo(UserInfo userInfo, QuanZiVo quanZiVo) {
        BeanUtil.copyProperties(userInfo, quanZiVo, "id");
        quanZiVo.setGender(userInfo.getSex().name().toLowerCase());
        quanZiVo.setTags(StringUtils.split(userInfo.getTags(), ","));

        // 当前用户
        User user = UserThreadLocal.get();

        quanZiVo.setCommentCount(0);// TODO 评论数
        quanZiVo.setDistance("1.2公里");// TODO 距离
        quanZiVo.setHasLiked(quanZiApi.queryUserIsLike(user.getId(), quanZiVo.getId()) ? 1 : 0);// 是否点赞(1是,  0否)
        quanZiVo.setLikeCount(Convert.toInt(quanZiApi.queryLikeCount(quanZiVo.getId())));// 点赞数
        quanZiVo.setHasLoved(quanZiApi.queryUserIsLove(user.getId(), quanZiVo.getId()) ? 1 : 0);// 是否喜欢(1是,0否)
        quanZiVo.setLoveCount(0);// TODO 喜欢数
    }

    /**
     * 封装 查询到的发布信息集合 到QuanZiVo对象集合
     *
     * @param records 查询到的发布信息集合
     * @return 封装完毕的QuanZiVo对象集合
     */
    private List<QuanZiVo> fillQuanZiVo(List<Publish> records) {
        // 查到发布内容的集合后,封装进QuanZiVo对象集合
        ArrayList<QuanZiVo> quanZiVoList = new ArrayList<>();
        records.forEach(publish -> {
            QuanZiVo quanZiVo = new QuanZiVo();
            quanZiVo.setId(publish.getId().toHexString());
            quanZiVo.setTextContent(publish.getText());
            quanZiVo.setImageContent(publish.getMedias().toArray(new String[]{}));
            quanZiVo.setUserId(publish.getUserId());
            quanZiVo.setCreateDate(RelativeDateFormat.format(new Date(publish.getCreated())));
            quanZiVoList.add(quanZiVo);
        });
        // 根据用户ID去MySQL查询用户信息(因为显示的发布内容中也包括用户信息)
        List<Object> userIdList = CollUtil.getFieldValues(quanZiVoList, "userId");
        List<UserInfo> userInfoList = userInfoService.queryUserInfoListByUserIdList(userIdList);
        for (QuanZiVo quanZiVo : quanZiVoList) {
            // 找到匹配的用户信息,进行封装
            for (UserInfo userInfo : userInfoList) {
                if (quanZiVo.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    fillUserInfoToQuanZiVo(userInfo, quanZiVo);
                    break;
                }
            }
        }
        return quanZiVoList;
    }

    /**
     * 发布动态
     *
     * @param textContent
     * @param location
     * @param latitude
     * @param longitude
     * @param multipartFile
     * @return
     */
    public String savePublish(String textContent,
                              String location,
                              String latitude,
                              String longitude,
                              MultipartFile[] multipartFile) {
        // 查询当前的登录信息
        User user = UserThreadLocal.get();
        Publish publish = new Publish();
        publish.setUserId(user.getId());
        publish.setText(textContent);
        publish.setLocationName(location);
        publish.setLatitude(latitude);
        publish.setLongitude(longitude);
        publish.setSeeType(1);
        List<String> picUrls = new ArrayList<>(); // 图⽚片上传
        for (MultipartFile file : multipartFile) {
            PicUploadResult picUploadResult = picUploadService.upload(file);
            picUrls.add(picUploadResult.getName());
        }
        publish.setMedias(picUrls);
        return quanZiApi.savePublish(publish);
    }

    public PageResult queryRecommendPublishList(Integer page, Integer pageSize) {
        // 分析:通过dubbo中的服务查询系统推荐动态 //通过mysql查询⽤用户的信息,回写到结果对象中(QuanZiVo)
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        // 直接从ThreadLocal中获取对象
        User user = UserThreadLocal.get();
        // 通过dubbo查询数据
        PageInfo<Publish> pageInfo = quanZiApi.queryRecommendPublishList(user.getId(), page, pageSize);
        List<Publish> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }
        pageResult.setItems(fillQuanZiVo(records));
        return pageResult;
    }

    public Long likeComment(String publishId) {
        User user = UserThreadLocal.get();
        // 点赞
        Boolean result = quanZiApi.likeComment(user.getId(), publishId);
        if (result) {
            // 查询点赞数
            return quanZiApi.queryLikeCount(publishId);
        }
        return null;
    }

    public Long disLikeComment(String publishId) {
        User user = UserThreadLocal.get();
        // 取消点赞
        Boolean result = quanZiApi.disLikeComment(user.getId(), publishId);
        if (result) {
            // 查询点赞数
            return quanZiApi.queryLikeCount(publishId);
        }
        return null;
    }

    public Long loveComment(String publishId) {
        User user = UserThreadLocal.get();
        // 喜欢
        Boolean result = quanZiApi.loveComment(user.getId(), publishId);
        if (result) {
            // 查询喜欢数
            return quanZiApi.queryLoveCount(publishId);
        }
        return null;
    }

    public Long disLoveComment(String publishId) {
        User user = UserThreadLocal.get();
        // 取消喜欢
        Boolean result = quanZiApi.disLoveComment(user.getId(), publishId);
        if (result) {
            // 查询喜欢数
            return quanZiApi.queryLoveCount(publishId);
        }
        return null;
    }

    public QuanZiVo queryById(String publishId) {
        Publish publish = quanZiApi.queryPublishById(publishId);
        if (publish == null) {
            return null;
        }
        return fillQuanZiVo(Arrays.asList(publish)).get(0);
    }

    /**
     * 查询评论列表
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult queryCommentList(String publishId, Integer page, Integer pageSize) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);

        User user = UserThreadLocal.get();

        //查询评论列表数据
        PageInfo<Comment> pageInfo = quanZiApi.queryCommentList(publishId, page, pageSize);
        List<Comment> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }

        //查询用户信息
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        List<UserInfo> userInfoList = userInfoService.queryUserInfoListByUserIdList(userIdList);

        List<CommentVo> result = new ArrayList<>();
        for (Comment record : records) {
            CommentVo commentVo = new CommentVo();
            commentVo.setContent(record.getContent());
            commentVo.setId(record.getId().toHexString());
            commentVo.setCreateDate(DateUtil.format(new Date(record.getCreated()), "HH:mm"));
            //是否点赞
            commentVo.setHasLiked(quanZiApi.queryUserIsLike(user.getId(), commentVo.getId()) ? 1 : 0);
            //点赞数
            commentVo.setLikeCount(Convert.toInt(quanZiApi.queryLikeCount(commentVo.getId())));


            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(record.getUserId(), userInfo.getUserId())) {

                    commentVo.setAvatar(userInfo.getLogo());
                    commentVo.setNickname(userInfo.getNickName());

                    break;
                }
            }

            result.add(commentVo);
        }

        pageResult.setItems(result);

        return pageResult;
    }

    /**
     * 发表评论
     *
     * @param publishId
     * @param content
     * @return
     */
    public Boolean saveComments(String publishId, String content) {
        User user = UserThreadLocal.get();
        return quanZiApi.saveComment(user.getId(), publishId, content);
    }

    /**
     * 根据用户id查询发布动态集合
     *
     * @param userId   用户id
     * @param page     页码
     * @param pageSize 页大小
     * @return PageResult
     */
    public PageResult queryAlbumList(Long userId, Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);

        // 查询相册表
        PageInfo<Publish> publishPageInfo = quanZiApi.queryAlbumList(userId, page, pageSize);
        List<Publish> publishList = publishPageInfo.getRecords();
        if (CollUtil.isEmpty(publishList)) {
            return pageResult;
        }

        // 填充
        List<QuanZiVo> quanZiVoList = fillQuanZiVo(publishList);
        pageResult.setItems(quanZiVoList);

        return pageResult;
    }

    public List<VisitorsVo> queryVisitorsList() {
        User user = UserThreadLocal.get();
        List<Visitors> visitorsList = visitorsApi.queryMyVisitor(user.getId());
        if (CollUtil.isEmpty(visitorsList)) {
            return Collections.emptyList();
        }

        List<Object> userIds = CollUtil.getFieldValues(visitorsList, "visitorUserId");
        List<UserInfo> userInfoList = userInfoService.queryUserInfoListByUserIdList(userIds);

        List<VisitorsVo> visitorsVoList = new ArrayList<>();

        for (Visitors visitor : visitorsList) {
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(visitor.getVisitorUserId(), userInfo.getUserId())) {
                    VisitorsVo visitorsVo = new VisitorsVo();
                    visitorsVo.setAge(userInfo.getAge());
                    visitorsVo.setAvatar(userInfo.getLogo());
                    visitorsVo.setGender(userInfo.getSex().name().toLowerCase());
                    visitorsVo.setId(userInfo.getUserId());
                    visitorsVo.setNickname(userInfo.getNickName());
                    visitorsVo.setTags(StringUtils.split(userInfo.getTags(), ','));
                    visitorsVo.setFateValue(visitor.getScore().intValue());

                    visitorsVoList.add(visitorsVo);
                    break;
                }
            }
        }
        return visitorsVoList;
    }
}